import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    swiperIndex: 0,
    devList: [],
    note: '',
    uploadUrl: wx.getStorageSync('uploadUrl'),
    recMac: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */

  getParamsByKey(url, key) {
    var paramsArr = decodeURIComponent(url).split('?');
    var keyValueArr = paramsArr[1].split("&");
    var size = keyValueArr.length;
    for (var i = 0; i < size; i++) {
      let splitItem = keyValueArr[i].split("=");
      if (key === splitItem[0]) {
        return splitItem[1]
      }
    }
  },

  onLoad: function (options) {
    if (options.typeId) {
      this.data.typeId = options.typeId
    }
    if (options.q) {
      console.log('options', options);
      var getUrl = wx.getStorageSync('redirectUrl');
      if (!getUrl) {
        wx.setStorageSync("redirectUrl", encodeURIComponent(options.q));
      }
      
      var g1 = this.getParamsByKey(options.q, 'g1');
      console.log('g1', g1);
      if (g1) {
        this.setData({
          recMac: g1
        })
      }
    }
    if (wx.getStorageSync('redirectUrl')) {
      this.findDeviceByMac()
      wx.removeStorageSync('redirectUrl')
    } else {
      this.getlist();
      this.getDeviceList();
    }

    if (options.note) {
      this.setData({
        note: options.note
      })
    }

    
  },

  findDeviceByMac: function () {
    _wxRequest.wxTonkenAjax({
      url: `${app.globalData.apiUrl}/gds/protected/device/findByMacType/${this.data.recMac}`,
      method: 'POST',
    }, (res) => {
      console.log(123, res.list[0].correct, res.list[0].id);
      this.getDeviceList(res.list[0].correct, res.list[0].id)
      this.setData({
        note: res.list[0].note
      })
    })
  },

  bindPickerChange: function (e) {
    var index = e.detail.value
    wx.navigateTo({
      url: '/pages/dye_paste/balance?typeId=' + this.data.devList[index].id,
    })
  },

  balanceHandle: function () {
    wx.navigateTo({
      url: '/pages/dye_paste/balance?typeId=' + this.data.devList[0].id,
    })
  },

  getlist: function () {
    var params = {
      typeId: this.data.typeId,
      level: 1,
      pageSize: 100
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/dyeColor/findList',
      method: 'POST',
      data: params
    }, (res) => {
      let list = res.list;
      console.log('list',list);
      this.setData({
        levelList: list
      });
    })
  },

  //获取设备列表
  getDeviceList: function (correct, typeId) {
    var params = {
      pageNo: 1,
      pageSize: 100,
      correct: correct + '' || '1',
      typeId: typeId || this.data.typeId
    }
    console.log(params, correct, typeId);
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/findList',
      method: 'POST',
      data: params
    }, (res) => {
      console.log('resss', res);
      let list = res.list;
      if (list.length > 1) {
        this.setData({
          mDevState: true
        })
      }
      if (this.data.recMac) {
        var devItem = list.filter(item => item.code == this.data.recMac)[0];
        wx.setStorageSync('devId', devItem.id)
        this.setData({
          typeId: devItem.typeId
        })
        this.getlist()
      }

      this.setData({
        devList: list
      })

    })
  },


  toChooseColor(e) {
    var index = e.currentTarget.dataset.index
    var item = e.currentTarget.dataset.item
    console.log('item', item)
    let url = "/pages/choose_colors/index"
    url = item.url || url
    wx.navigateTo({
      url: url+'?idx=' + index + '&id=' + item.id + '&typeId=' + this.data.typeId,
      success: (res) => {
        res.eventChannel.emit('currentItem', item)
      }
    })
  }

})